using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml;
using System.Data;

namespace BuildProperties
{
    public class Program
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("Press ENTER to start...");
            Console.ReadLine();

            // Code
            Program.InitializePropertyTemplateBody();
            Program.ReadContactMappings();
            Program.InitializePropertyFile();
            Program.AddProperties();
            Program.ClosePropertyFile();

            Console.WriteLine("Finished...press ENTER to exit...");
            Console.ReadLine();
        }

        private static string propertyMappingsFileName = @"I:\Consulting\Exchange.NET\Code\MIS.EN\MIS.EN.Exchange\TaskPropertyMappings.xml";
        private static string propertyFileName = "properties.txt";
        private static string propertyTemplateFileName = @"I:\Consulting\Exchange.NET\Code\MIS.EN\BuildProperties\template.txt";
        private static string propertyTemplateBody = string.Empty;
        private static StreamWriter writer = null;
        private static XmlNodeList propertyNodes = null;

        private static void InitializePropertyFile()
        {
            Program.writer = new StreamWriter(Program.propertyFileName, true);
        }

        private static void ClosePropertyFile()
        {
            Program.writer.Close();
        }

        private static void InitializePropertyTemplateBody()
        {
            using (StreamReader reader = new StreamReader(Program.propertyTemplateFileName))
            {
                Program.propertyTemplateBody = reader.ReadToEnd();
            }
        }

        private static void ReadContactMappings()
        {
            XmlDocument contactMappings = new XmlDocument();
            contactMappings.Load(Program.propertyMappingsFileName);
            Program.propertyNodes = contactMappings.SelectNodes("/propertyMappings/property");
        }

        private static void AddProperty(string propertyName, string privateVariableName)
        {
            Program.writer.WriteLine(propertyTemplateBody.Replace("{0}", privateVariableName).Replace("{1}", propertyName));
            Program.writer.WriteLine();
        }

        private static void AddProperties()
        {
            string propertyName = string.Empty;
            string privateVariableName = string.Empty;

            foreach (XmlNode node in Program.propertyNodes)
            {
                // Get the property name
                propertyName = node.Attributes.GetNamedItem("name").Value;
                // Get the camel-case property name (which will be the private variable for the property)
                privateVariableName = propertyName.Replace(propertyName[0], propertyName[0].ToString().ToLower().ToCharArray()[0]);
                // Add the property
                Program.AddProperty(propertyName, privateVariableName);
            }
        }
    }
}
